// const baseURL = process.env.VUE_APP_BASE_API;
import { Loading } from "element-ui";
import axios from "axios";
// 回显数据字典
export function selectDictLabel(datas, value) {
  var actions = [];
  Object.keys(datas).map(id => {
    if (datas[id].itemValue == "" + value) {
      actions.push(datas[id].itemText);
      return false;
    }
  });
  return actions.join("");
}

// 通用下载方法
// export function download(fileName) {
//   window.location.href =
//     axios.defaults.baseURL +
//     "/common/download?fileName=" +
//     encodeURI(fileName) +
//     "&delete=" +
//     true;
// }

//手机正则验证
export function checkPhone(phone) {
  if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone)) {
    alert("手机号码有误，请重填");
    return false;
  }
}
//验证是否数字
export function isNumber(o) {
  return Object.prototype.toString.call(o).slice(8, -1) === "Number";
}

//GTM时间格式转为普通格式
export function GMTToStr(time) {
  let date = new Date(time);
  let Str =
    date.getFullYear() +
    "-" +
    (date.getMonth() + 1) +
    "-" +
    date.getDate() +
    " " +
    date.getHours() +
    ":" +
    date.getMinutes() +
    ":" +
    date.getSeconds();
  return Str;
}
/**
 *
 * @param {*下载方式} http
 * @param {*链接地址} pathUrl
 * @param {*导出请求参数} searchList
 * @param {*下载的名字} xlsName
 */
export function expotOut(http, pathUrl, searchList, xlsName) {
  // 获取时间，这一步是在下载时文件名带下载日期，例如：用户信息2020-04-27.xls,如无需要可以去掉
  let d = new Date();
  let month = d.getMonth() + 1;
  let day = d.getDate();
  let time =
    d.getFullYear() +
    "-" +
    (String(month).length > 1 ? month : "0" + month) +
    "-" +
    (String(day).length > 1 ? day : "0" + day);
  // 地址
  let baseURL = axios.defaults.baseURL; // 域名
  const PATH = {
    userList: pathUrl // 后台接口地址
  };
  // 参数
  let params = "";
  for (let key in searchList) {
    if (searchList[key] == null) {
      delete searchList[key];
    }
  }
  let arr = Object.keys(searchList);
  if (arr.length !== 0) {
    params = "?";
  }
  for (let key in searchList) {
    params = params + key + "=" + searchList[key] + "&";
  }
  function createObjectURL(object) {
    return window.URL
      ? window.URL.createObjectURL(object)
      : window.webkitURL.createObjectURL(object);
  }
  var xhr = new XMLHttpRequest();
  var formData = new FormData();
  if( http =="get" ){
    xhr.open("get", baseURL + PATH.userList + params); // url填写后台的接口地址
  }else{
    xhr.open("post", baseURL + PATH.userList);//如果是post，在formData append参数
    Object.keys(searchList).map(key => {
      formData.append(key, searchList[key]);
    });;
  }
  xhr.setRequestHeader("INVALID_SIGN", sessionStorage.getItem("token"));
  xhr.responseType = "blob";
  xhr.onload = function() {
    if (this.status === 200) {
      var blob = this.response;
      // xls文件名称
      var filename = `${xlsName}${time}.xls`;
      if (window.navigator.msSaveOrOpenBlob) {
        navigator.msSaveBlob(blob, filename);
      } else {
        var a = document.createElement("a");
        var url = createObjectURL(blob);
        a.href = url;
        a.download = filename;
        document.body.appendChild(a);
        a.click();
        window.URL.revokeObjectURL(url);
      }
    } else {
      alert("导出失败");
    }
  };
  xhr.send(formData);
}

//动画加载loading
export function loadingFull() {
  return Loading.service({
    lock: true,
    text: "Loading",
    spinner: "el-icon-loading",
    background: "rgba(0, 0, 0, 0.7)"
  });
}

export function checkPermiss(code, arr) {
  let index = arr.indexOf(code);
  if (index == -1) {
    return false;
  } else {
    return true;
  }
}
